/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package map;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author sfsuarez
 * 
 * Modulo que ejecuta sentencias SQL de actualización  de la BD de Absysnet
 * Se utiliza para diferentes propositos.
 * 10/05/2013
 */
public class Map {
    
    public static String host = null;
    public static String usuario = null;
    public static String clave = null; 
   // public static String path = "C:\\Users\\sfsuarez\\Documents\\NetBeansProjects\\map\\";
    public static String path = "E:\\ABSYSNET\\Admin";
     public static ResultSet rs = null;
     public static ResultSet rs2 = null;
     public static absconect absysconex = null;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException {
        String datos[] = data_cfn("absysNET").split(",");
        
         absysconex = new absconect();
         if( absysconex.conectar(datos[0], datos[1], datos[2], datos[3]) ){
             
             String dat = data_cfn("sentences"); 
             
             System.out.print(dat);
             
         }
    }//end Map
    
    public static String  data_cfn(String fil ){
        String attr=null;
      try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new File( path + fil+".xml"));
            doc.getDocumentElement().normalize();
            
            if( fil.equals("absysNET")){
                    attr = get_attr (doc);
            }else{
                   attr =  update_depoit( doc ) + "  datos actualizados";
            }
             
            
            //System.out.println( attr );
            
            
        } catch (ParserConfigurationException e) {
                e.printStackTrace();
        } catch (SAXException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        }
        
      return attr;
    
    
    }//end 
    
    public static  String get_attr( Document doc ){
        String tags[] = {"sql","oci"};
        String datos = "";
        for( int x = 0; x < tags.length; x++){
                                  
        
                    NodeList nodosRaiz = doc.getDocumentElement().getElementsByTagName( tags[x]);
                    NodeList nodoshijos = nodosRaiz.item(0).getChildNodes();
                    for(int i = 0; i < 4; i++) {
                       Node nodo = nodoshijos.item(i);  

                            if( nodo.getNodeType() == Node.ELEMENT_NODE  ){                        

                                    NamedNodeMap attr = nodo.getAttributes();
                                    Node nodeAttr = attr.getNamedItem("value");
                                    datos += nodeAttr.getNodeValue()+ ",";
                                   // System.out.println(  nodo.getNodeName() + "|" + nodeAttr.getNodeValue() ) ;



                            }
                    }
         }
        return datos;
    
    }
    
    public static int update_depoit ( Document doc ){
           String datos = "";
           int no = 0;
           NodeList nodosRaiz = doc.getDocumentElement().getElementsByTagName( "stm");
                      
            NodeList nodoshijos = nodosRaiz.item(0).getChildNodes();

            for(int i = 0; i < nodosRaiz.getLength() ; i++) {
                Node nodo = nodosRaiz.item(i);  
                 if (nodo.getNodeType() == Node.ELEMENT_NODE) {

                     Element eElement = (Element) nodo;

                      NodeList nlList= eElement.getElementsByTagName("sql").item(0).getChildNodes();
                        Node nValue = (Node) nlList.item(0);
                        
                        no += absysconex.update( nValue.getNodeValue().replace("!=", "<>") );
 
                 }

            }

           return no;
    }
}//end class
